package io.renren.modules.car.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.IFNULL;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.car.entity.CarBaseEntity;
import io.renren.modules.car.entity.CarSellMsgEntity;
import io.renren.modules.car.entity.CarShopMoneyEntity;
import io.renren.modules.car.service.CarSellMsgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 车辆售卖信息
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-09-17 11:28:02
 */
@RestController
@RequestMapping("car/carsellmsg")
public class CarSellMsgController {
    @Autowired
    private CarSellMsgService carSellMsgService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("car:carsellmsg:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = carSellMsgService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("car:carsellmsg:info")
    public R info(@PathVariable("id") Integer id){
        CarSellMsgEntity carSellMsg = carSellMsgService.getById(id);

        return R.ok().put("carSellMsg", carSellMsg);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("car:carsellmsg:save")
    public R save(@RequestBody CarSellMsgEntity carSellMsg){
        Date date = new Date();
        carSellMsg.setCreateTime(date);
        carSellMsg.setUpdateTime(date);
        carSellMsgService.save(carSellMsg);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("car:carsellmsg:update")
    public R update(@RequestBody CarSellMsgEntity carSellMsg){
        ValidatorUtils.validateEntity(carSellMsg);
        Date date = new Date();
        carSellMsg.setUpdateTime(date);
        carSellMsgService.updateById(carSellMsg);
        
        return R.ok();
    }


    /**
     * 修改价格相关
     */
    @RequestMapping("/updateMoneyMsg")
    @RequiresPermissions("car:carbase:update")
    public R updateMoneyMsg(@RequestBody CarBaseEntity carBaseEntity){
        ValidatorUtils.validateEntity(carBaseEntity);

        boolean b = carSellMsgService.updateMoneyMsg(carBaseEntity);

        return R.ok();
    }


    /**
     * 修改售出状态
     */
    @RequestMapping("/updateState")
    @RequiresPermissions("car:carsellmsg:updateState")
    public R updateState(@RequestBody JSONObject jsonObject){
        Integer state = jsonObject.getInteger("state");
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        ValidatorUtils.validateEntity(ids);
        ValidatorUtils.validateEntity(state);

        Collection<CarSellMsgEntity> carSellMsgEntities = carSellMsgService.listByIds(ids);
        for (CarSellMsgEntity c :carSellMsgEntities) {
            if(Objects.isNull(c.getLastMoney())){
                return R.error("请先保存价格相关的数据");
            }
        }

        List<CarSellMsgEntity> carSellMsgEntityList = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            CarSellMsgEntity carSellMsgEntity = new CarSellMsgEntity();
            carSellMsgEntity.setId(ids.get(i));
            carSellMsgEntity.setState(state);
            carSellMsgEntity.setUpdateTime(new Date());
            carSellMsgEntityList.add(carSellMsgEntity);

        }
        carSellMsgService.updateBatchById(carSellMsgEntityList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("car:carsellmsg:delete")
    public R delete(@RequestBody Integer[] ids){
        carSellMsgService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
